package code.ws_common_scene.mode
{
	import code.ws_common_ems.ModeStatusEms;
	import code.ws_common_scene.command.HeroCommandDevice;
	import code.ws_common_scene.command.cmd.AttackCommand;
	import code.ws_common_scene.data.HpDamage;
	import code.ws_common_scene.data.base.Damage;
	import code.ws_common_scene.manager.SceneManager;
	import code.ws_common_scene.map.MapData;
	import code.ws_common_scene.mode.base.Fighter;
	import code.ws_common_utils.MapUtils;
	import code.ws_common_utils.MathUtils;
	
	import flash.geom.Point;
	
	import starling.core.Starling;

	/**
	 * 主角模型 
	 * @author qiangchen
	 * 
	 */	
	public class Hero extends Fighter
	{
		/** 路径 */
		private var _movePath:Array = null;
		
		/**
		 * 构造函数 
		 * @param render
		 * @param style
		 * @param action
		 * @param dir
		 * 
		 */		
		public function Hero( render:String, style:String, action:String, dir:int )
		{
			super( render, style, action, dir );
		}
		
		
		/**
		 * 移动 
		 * @param path
		 * 
		 */		
		override public function walk( path:Array = null ):void
		{
			if( path == null || path.length == 0 )
			{
				// 路径不存在，或者路径长度为0
				return;
			}
			
			// 释放第一个路点，因为此路点就是当前英雄的起始位置
			_movePath = path.slice( 1 );
			if( _movePath.length == 0 )
			{
				// 原地待命
				return;
			}
			
			if( !(this.status == ModeStatusEms.WALK) )
			{
				goto_p();
			}
			super.walk( _movePath );
		}
		
		
		private function goto_p():void
		{
			Starling.juggler.removeTweens( this );
			
			var mapData:MapData = SceneManager.instance.scene.mapData;
			var ary:Array = _movePath.shift();
			var tar_p:Point = new Point(ary[0], ary[1]);
			var tar_p_pixel:Point = MapUtils.tileToPixel( tar_p, mapData ) ;
			var flag:Boolean = mapData.isShadow( tar_p.x, tar_p.y );
			
			// 设置移动转向
			var dir:int = MathUtils.getRealDirByTile( this.tiles, tar_p );
			this.setDirection( dir );
			
			// 移动
			var moveTime:Number = MathUtils.getMoveTime( dir, this.moveSpeed );
			tar_p_pixel.x += mapData.hTileW;
			tar_p_pixel.y += mapData.hTileH;
			Starling.juggler.tween( this, moveTime, {x:tar_p_pixel.x, y:tar_p_pixel.y, onUpdate:walkIng, onComplete:walkOver} );
			
			// 设置角色逻辑位置
			this.tiles = tar_p;
			// 设置阴影
			this.alpha = ( flag ) ? 0.5 : 1;//BlendMode.OVERLAY : BlendMode.NORMAL;
			
			// 释放
			mapData = null;
			ary.length = 0;
			ary = null;
			tar_p = null;
			tar_p_pixel = null;
			dir = NaN;
			moveTime = NaN;
		}
		
		
		/**
		 * 移动中 
		 * 
		 */		
		private function walkIng():void
		{
			// 滚动地图
			SceneManager.instance.scrollMap( this.x, this.y );
		}
		
		
		
		/**
		 * 移动结束 
		 * 
		 */		
		private function walkOver():void
		{
			// 判断路径点是否都已走完
			if( _movePath == null || _movePath.length == 0 )
			{
				stand();
				Starling.juggler.removeTweens( this );
				
				// 释放指令
				if( HeroCommandDevice.instance.crtCommand )
				{
					HeroCommandDevice.instance.crtCommand.over();
				}
				return;
			}
			
			// 继续下一个点
			goto_p();
		}
		
		
		/**
		 * 攻击关键帧 
		 * 
		 */		
		override protected function attackKey():void
		{
			// 震屏
			if( _attackData )
			{
				if( _attackData._damage._type ==  Damage.HURT_HP )
				{
					if( ( _attackData._damage as HpDamage ).isCrit )
					{
						SceneManager.instance.vibrationScreen();
					}
				}
			}
			
			// 继承上一级指令
			super.attackKey();
			
			// 继续针对目标进行攻击
			HeroCommandDevice.instance.updataCommand( new AttackCommand() );
		}
		
		
		/**
		 * 释放 
		 * 
		 */		
		override public function dispose():void
		{
			if( _movePath )
			{
				_movePath.length = 0;
				_movePath = null;
			}
			Starling.juggler.removeTweens( this );
			super.dispose();
		}
	}
}